package com.zzb.service.es;

import com.zzb.dao.UserDao;
import com.zzb.entity.UserEntity;
import com.zzb.repostitory.UserRepository;
import io.netty.util.internal.StringUtil;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ESUserService {

    @Autowired
    private UserRepository userRepository;
    @Autowired
    private UserDao userDao;
    @Autowired
    private UserEntity userEntity;

    /**
     * 将数据保存到es
     */
    public void save() {
        List<UserEntity> userEntityList = userDao.findAll(userEntity);
        userRepository.saveAll(userEntityList);
    }

    /**
     * 从es获取数据
     *
     * @return
     */
    public List<UserEntity> findAll() {
        Iterable<UserEntity> userEntities = userRepository.findAll();
        List<UserEntity> userEntityList = new ArrayList<>();
        userEntities.forEach(userEntityList::add);
        return userEntityList;
    }

    /**
     * 条件判断
     *
     * @param userEntity 实体类
     * @return
     */
    public List<Object> determine(UserEntity userEntity) {
        List<Object> determine = new ArrayList<>();
        //高亮显示的样式
        HighlightBuilder highlight = new HighlightBuilder();
        highlight.preTags("<font color='red'>")
                .postTags("</font>");
        //构建条件查询语句
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();

        //判断username不为null或""
        if (!StringUtil.isNullOrEmpty(userEntity.getUsername())) {
            boolQueryBuilder.must(QueryBuilders.matchQuery("username", userEntity.getUsername()));
            highlight.field("username");
            //determine.add(userEntity.getUsername());
        }
        //判断email
        if (!StringUtil.isNullOrEmpty(userEntity.getEmail())) {
            boolQueryBuilder.must(QueryBuilders.matchQuery("email", userEntity.getEmail()));
            highlight.field("email");
            //determine.add(userEntity.getEmail());
        }
        //判断sex
        if (!StringUtil.isNullOrEmpty(userEntity.getSex())) {
            boolQueryBuilder.must(QueryBuilders.matchQuery("sex", userEntity.getSex()));
            //highlight.field("sex");
            //determine.add(userEntity.getSex());
        }
        //判断enable
        if (userEntity.getEnable() != null && userEntity.getEnable() != -1) {
            boolQueryBuilder.must(QueryBuilders.matchQuery("enable", userEntity.getEnable()));
            //highlight.field("enable");
            //determine.add(userEntity.getEnable());
        }
        //判断address
        if (!StringUtil.isNullOrEmpty(userEntity.getAddress())) {
            boolQueryBuilder.must(QueryBuilders.matchQuery("address", userEntity.getAddress()));
            highlight.field("address");
            //determine.add(userEntity.getEnable());
        }
        determine.add(boolQueryBuilder);
        determine.add(highlight);
        return determine;
    }
}
